package com.oa.ylms.apps.dao.goods.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.goods.IGoodsInfoStates;
import com.oa.ylms.apps.dao.goods.IGoodsInfoDao;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;
import com.oa.ylms.apps.entity.goods.GoodInfoValue;

public class GoodsInfoDaoImpl extends HibernateDaoSupport implements
		IGoodsInfoDao {
	private List<GoodInfoValue> infoList;
	private int numbers;

	@SuppressWarnings("unchecked")
	public List<GoodInfoValue> queryGoodInfoWithPage(
			final GoodInfoValue goodInfo, final int rows, final int pageNo)
			throws Exception {
		String hqlStr = "FROM GoodInfoValue giv WHERE 1=1 ";
		if (goodInfo.getGoodCategory() != null) {
			hqlStr += " AND giv.goodCategory = ?";
		}
		if (goodInfo.getStates() != null) {
			hqlStr += " AND giv.states = ?";
		} else {
			hqlStr += " AND giv.states<>-1";
		}
		hqlStr += " order BY giv.createTime DESC";
		final String hql = hqlStr;
		System.out.println("hql:" + hql);
		infoList = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						int index = -1;
						if (goodInfo.getGoodCategory() != null) {
							query.setEntity(++index, goodInfo.getGoodCategory());
						}
						if (goodInfo.getStates() != null) {
							query.setInteger(++index, goodInfo.getStates());
						}
						if (rows != -1) {
							query.setFirstResult(rows * (pageNo - 1));
							query.setMaxResults(rows);
						}
						infoList = query.list();
						return infoList;
					}
				});
		return infoList;

	}

	public int addGoodsInfo(GoodInfoValue info) {
		if (info != null) {
			try {
				this.getSession().setFlushMode(FlushMode.AUTO);
				this.getHibernateTemplate().save(info);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return 0;
		}
		return 0;
	}

	public GoodInfoValue queryByGoodId(GoodInfoValue goodInfo) {
		if (goodInfo != null) {
			return (GoodInfoValue) this.getHibernateTemplate().get(
					GoodInfoValue.class, goodInfo.getGoodId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodInfoValue> queryAllByCateId(GoodCategoryValue goodCategory,
			int role) {
		String hql = "";
		if (goodCategory != null) {
			if (role == IGoodsInfoStates.FORMANAGER) {
				hql = "from GoodInfoValue giv  where giv.states<>-1 and giv.goodCategory=? order by giv.goodCategory";
				return this.getHibernateTemplate().find(hql, goodCategory);
			}
			if (role == IGoodsInfoStates.FORUSER) {
				hql = "from GoodInfoValue giv  where giv.states = 1 and giv.goodCategory=? order by giv.goodCategory";
				return this.getHibernateTemplate().find(hql, goodCategory);
			}
		}
		return null;
	}

	@Override
	public boolean editGoodsInfo(GoodInfoValue info) {

		if (info != null) {
			try {
				this.getHibernateTemplate().update(info);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public int countTpages(final GoodInfoValue goodInfo, int rows)
			throws Exception {

		int pageNumbers = 0;
		String hqlStr = "FROM GoodInfoValue giv WHERE 1=1";
		if (goodInfo.getGoodCategory() != null) {

			hqlStr += " AND giv.goodCategory = ?";
		}
		if (goodInfo.getStates() != null) {

			hqlStr += " AND giv.states = ?";
		} else {
			hqlStr += " AND giv.states<>-1";
		}
		hqlStr += " ORDER BY giv.goodCategory";
		final String hql = hqlStr;
		System.out.println("hql:" + hql);
		infoList = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						int index = -1;
						if (goodInfo.getGoodCategory() != null) {
							query
									.setEntity(++index, goodInfo
											.getGoodCategory());
						}
						if (goodInfo.getStates() != null) {
							query.setInteger(++index, goodInfo.getStates());
						}
						infoList = query.list();
						return infoList;
					}
				});
		numbers = infoList.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;

	}

	public List<GoodInfoValue> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<GoodInfoValue> infoList) {
		this.infoList = infoList;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

	@Override
	public int getTotalNumber() {
		return numbers;
	}

}
